package com.llsydn.a06copyviewpager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author lilinshen
 * @title 仿viewpager控件
 * @description 测量--绘制--构造
 * @date 2018/10/16 15:28
 */
public class MyViewPager extends ViewGroup {

    /**
     * 手势识别器
     * 1.定义
     * 2.实例化-把想要的方法给重新
     * 3.在onTouchEvent()把事件传递给手势识别器
     */
    private GestureDetector detector;
    private MyScroller scroller;
    private float startX;
    private int currentIndex;
    private OnPageChangListener onPageChangListener;

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public void initView(Context context) {
        scroller = new MyScroller();
        //实例化手势识别器
        detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public void onLongPress(MotionEvent e) {
                super.onLongPress(e);
            }

            /**
             * 滑动
             * @param e1
             * @param e2
             * @param distanceX 在x轴滑动的距离
             * @param distanceY 在y轴滑动的距离
             * @return
             */
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                scrollBy((int) distanceX, 0);
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return super.onDoubleTap(e);
            }
        });
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //遍历孩子，给每个孩子指定在屏幕的坐标位置
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            childView.layout(i * getWidth(), 0, (i + 1) * getWidth(), getHeight());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        //把事件传递给手势识别器
        detector.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //1.记录坐标
                startX = event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                //2.来到新的坐标
                float endX = event.getX();
                //3.计算偏移量
                int tempIndex = currentIndex;
                if ((startX - endX) > getWidth() / 2) {
                    tempIndex++;
                } else if ((endX - startX) > getWidth() / 2) {
                    tempIndex--;
                }
                //根据下标位置移动到指定页面
                scrollToPage(tempIndex);
                break;
        }

        return true;
    }

    /**
     * 根据下标位置移动到指定页面
     *
     * @param tempIndex
     */
    public void scrollToPage(int tempIndex) {
        if (tempIndex < 0) {
            tempIndex = 0;
        }
        if (tempIndex > getChildCount() - 1) {
            tempIndex = getChildCount() - 1;
        }
        currentIndex = tempIndex;
        //使用监听器
        if (onPageChangListener != null) {
            onPageChangListener.onScrollToPager(currentIndex);
        }

        int distanceX = currentIndex * getWidth() - getScrollX();
        scroller.startScroll(getScrollX(), getScrollY(), distanceX, 0);
        invalidate(); //onDraw()
    }

    @Override
    public void computeScroll() {
        if (scroller.cuputeScrollOffset()) {
            float currX = scroller.getCurrX();
            scrollTo((int) currX, 0);
            invalidate();
        }
    }

    /**
     * @author lilinshen
     * @title 监听页面的改变的接口
     * @description 请填写相关描述
     * @date 2018/10/17 10:36
     */
    public interface OnPageChangListener {
        /**
         * 当页面改变的时候回调这个方法
         *
         * @param position 当前页面的下标
         */
        void onScrollToPager(int position);
    }

    /**
     * 设置页面改变的监听
     *
     * @param listener
     */
    public void setOnPagerChangListener(OnPageChangListener listener) {
        onPageChangListener = listener;
    }

    /**
     * 因为已经是人为的指定了位置，所以测量可以不写
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        System.out.println(size + "--" + mode);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        System.out.println(sizeHeight + "--" + modeHeight);
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
